// 车辆违规管理模拟数据
const Mock = require('mockjs')

// 生成违规记录数据
const violations = Mock.mock({
  'records|20': [{
    'id|+1': 1,
    'plateNumber': /[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{5,6}/,
    'violationType|1': ['1', '2', '3', '4', '5', '6'],
    'violationTime': '@datetime("yyyy-MM-dd HH:mm:ss")',
    'description': '@csentence(10, 50)',
    'fine|50-500': 50,
    'status|1': ['0', '1', '2'],
    'handleType|1-3': 1,
    'handleTime': function() {
      return this.status === '0' ? null : Mock.Random.datetime('yyyy-MM-dd HH:mm:ss');
    },
    'remark': function() {
      return this.status === '0' ? null : Mock.Random.csentence(5, 20);
    },
    'createTime': '@datetime("yyyy-MM-dd HH:mm:ss")',
    'updateTime': '@datetime("yyyy-MM-dd HH:mm:ss")'
  }]
})

// 获取违规记录列表
Mock.mock('/violation/list', 'post', (options) => {
  const { body } = options
  let params = {}
  
  try {
    params = JSON.parse(body)
  } catch (e) {
    params = {}
  }

  const pagenum = params.pagenum || 1
  const pageSize = params.pageSize || 10
  
  let filteredRecords = [...violations.records]
  
  // 根据查询条件过滤
  if (params.plateNumber) {
    filteredRecords = filteredRecords.filter(item => 
      item.plateNumber.includes(params.plateNumber)
    )
  }
  
  if (params.violationType) {
    filteredRecords = filteredRecords.filter(item => 
      item.violationType === params.violationType
    )
  }
  
  if (params.status) {
    filteredRecords = filteredRecords.filter(item => 
      item.status === params.status
    )
  }
  
  // 计算分页
  const total = filteredRecords.length
  const start = (pagenum - 1) * pageSize
  const end = pagenum * pageSize
  const records = filteredRecords.slice(start, end)
  
  return {
    flag: true,
    code: 200,
    message: '获取成功',
    data: {
      records,
      total,
      pagenum,
      pageSize
    }
  }
})

// 添加违规记录
Mock.mock('/violation/add', 'post', (options) => {
  const { body } = options
  let params = {}
  
  try {
    params = JSON.parse(body)
  } catch (e) {
    return {
      flag: false,
      code: 400,
      message: '参数错误'
    }
  }
  
  // 验证必填字段
  if (!params.plateNumber || !params.violationType || !params.violationTime || !params.description) {
    return {
      flag: false,
      code: 400,
      message: '请填写完整的违规信息'
    }
  }
  
  // 生成新的违规记录
  const newViolation = {
    id: violations.records.length + 1,
    plateNumber: params.plateNumber,
    violationType: params.violationType,
    violationTime: params.violationTime,
    description: params.description,
    fine: params.fine || 50,
    status: params.status || '0',
    handleType: null,
    handleTime: null,
    remark: null,
    createTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss'),
    updateTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
  }
  
  // 添加到记录中
  violations.records.unshift(newViolation)
  
  return {
    flag: true,
    code: 200,
    message: '添加成功',
    data: newViolation
  }
})

// 处理违规记录
Mock.mock('/violation/handle', 'post', (options) => {
  const { body } = options
  let params = {}
  
  try {
    params = JSON.parse(body)
  } catch (e) {
    return {
      flag: false,
      code: 400,
      message: '参数错误'
    }
  }
  
  // 验证必填字段
  if (!params.id || !params.handleType || !params.status) {
    return {
      flag: false,
      code: 400,
      message: '请填写完整的处理信息'
    }
  }
  
  // 查找要处理的违规记录
  const index = violations.records.findIndex(item => item.id === params.id)
  
  if (index === -1) {
    return {
      flag: false,
      code: 404,
      message: '未找到该违规记录'
    }
  }
  
  // 更新记录
  violations.records[index] = {
    ...violations.records[index],
    status: params.status,
    handleType: params.handleType,
    handleTime: params.handleTime || Mock.Random.datetime('yyyy-MM-dd HH:mm:ss'),
    remark: params.remark || '',
    updateTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
  }
  
  return {
    flag: true,
    code: 200,
    message: '处理成功',
    data: violations.records[index]
  }
})

// 删除违规记录
Mock.mock(/\/violation\/delete\?id=\d+/, 'get', (options) => {
  const url = options.url
  const id = parseInt(url.split('=')[1])
  
  if (!id) {
    return {
      flag: false,
      code: 400,
      message: '参数错误'
    }
  }
  
  // 查找要删除的违规记录
  const index = violations.records.findIndex(item => item.id === id)
  
  if (index === -1) {
    return {
      flag: false,
      code: 404,
      message: '未找到该违规记录'
    }
  }
  
  // 删除记录
  violations.records.splice(index, 1)
  
  return {
    flag: true,
    code: 200,
    message: '删除成功'
  }
})

export default {
  violations
} 